#ifndef FileTransferManager_H
#define FileTransferManager_H

#include <map>

#include <wx/file.h>
#include <wx/filename.h>

class MsnSession;

class P2PMessage;

class FileTransferEntry;

typedef std::map<wxString, FileTransferEntry*> FileTransferEntryMap;

class FileTransferManager
{
private:
	MsnSession* session;

	FileTransferEntryMap entryMap;

	wxMutex entryMapMutex;

public:
	FileTransferManager(MsnSession* s);
	~FileTransferManager();

	void AddEntry(const wxString& id, FileTransferEntry* entry);
	void DeleteEntry(const wxString& id);
	FileTransferEntry* GetEntry(const wxString& id);

	wxString FindSessionID(const wxString& callID);

	void CancelTransfer(FileTransferEntry* item);

	void CancelTransfer(const wxString& sessionID);

	bool WriteFileData(P2PMessage* msg);

	bool ReadFileData(const wxString& sessionID, char* buf, int size, int& actualRead);

	void NotifyUpdatedEvent(const wxString& cookie, int transferredSize);

	void NotifyStartedEvent(const wxString& cookie);
	void NotifyFinishedEvent(const wxString& cookie);
	void NotifyFailedEvent(const wxString& cookie);

};


class FileTransferEvent : public wxCommandEvent
{
DECLARE_DYNAMIC_CLASS( FileTransferEvent )

private:
    MsnSession* session;

	wxString cookie;
	int transferSize;
	int transferSpeed;

public:
	FileTransferEvent( wxEventType t = wxEVT_NULL, int id=0) : wxCommandEvent(t,id), transferSize(0),transferSpeed(0)
	{
		
	}

	FileTransferEvent(const FileTransferEvent& e) 
		: wxCommandEvent(e)
	{
		SetSession(e.session);
		SetCookie(e.cookie);
		SetTransferSize(e.transferSize);
		SetTransferSpeed(e.transferSpeed);
	}

	virtual wxEvent* Clone() const {return new FileTransferEvent(*this);}

public:

    void SetSession(MsnSession* _session){session = _session;}

	void SetCookie(const wxString& _cookie){cookie = _cookie;}
	void SetTransferSize(int size){transferSize = size;}
	void SetTransferSpeed(int speed){transferSpeed = speed;}

	MsnSession* GetSession(){return session;}

	wxString GetCookie(){return cookie;}
	int GetTransferSize(){return transferSize;}
	int GetTransferSpeed(){return transferSpeed;}

};


typedef void (wxEvtHandler::*FileTransferEventFunction)(FileTransferEvent&);

#define EVT_FILE_TRANSFER(eventType, fn) \
    DECLARE_EVENT_TABLE_ENTRY( \
        eventType, wxID_ANY, wxID_ANY, \
        (wxObjectEventFunction)   \
        (FileTransferEventFunction) &fn, \
        (wxObject *) NULL \
    ),


#endif

